#pragma once
#include <stdexcept>

inline unsigned fib2(unsigned n)
{
    if (n < 1 || n > 40)
        throw std::runtime_error("Invalid input value. Value should be in range [1, 40].");

    if (n == 1)
        return 1;

    unsigned n_1 = 1;
    unsigned n_2 = 0;

    while (--n > 0)
    {
        const unsigned tmp = n_1;

        n_1 = n_1 + n_2;
        n_2 = tmp;
    }

    return n_1;
}
